package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ForgetInWordActivity extends BaseActivity implements View.OnClickListener{

    private ImageView inwordback;
    private ImageView wordclear;
    private ImageView wordtoclear;
    private EditText inword;
    private EditText inwordto;
    private Button surebt;
    private String code;
    private String phone;
    private String messageId;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_in_word);
        Intent intent = getIntent();
        code = intent.getStringExtra("code");
        phone = intent.getStringExtra("phone");
        messageId = intent.getStringExtra("messageId");
        initForgetInWordView();
    }

    private void initForgetInWordView() {
        inwordback = (ImageView) findViewById(R.id.forgetinword_back);
        wordclear = (ImageView) findViewById(R.id.forgetinword_wordclear);
        wordtoclear = (ImageView) findViewById(R.id.forgetinword_wordtoclear);
        inword = (EditText) findViewById(R.id.forgetinword_word);
        inwordto = (EditText) findViewById(R.id.forgetinword_wordto);
        surebt = (Button) findViewById(R.id.forgetinword_surebt);
        backmain = (ImageView) findViewById(R.id.forgetword_backmain);
        backmain.setOnClickListener(this);
        inwordback.setOnClickListener(this);
        wordclear.setOnClickListener(this);
        wordtoclear.setOnClickListener(this);
        surebt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forgetword_backmain:
                startActivity(new Intent(ForgetInWordActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();

                break;
            case R.id.forgetinword_back:
                finish();
                break;
            case R.id.forgetinword_wordclear:
                inword.setText("");
                break;
            case R.id.forgetinword_wordtoclear:
                inwordto.setText("");
                break;
            case R.id.forgetinword_surebt:
                String word = String.valueOf(inword.getText());
                String wordto = String.valueOf(inwordto.getText());
                if (word.equals("")){
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    if (wordto.equals("")){
                        Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();

                    }else {
                        if (word.equals(wordto)){
                            try {
                                String s = AESUtil.encryptAES(word);
                                findtoword(s);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }else {

                            Toast.makeText(this, "两次密码不一致，请重新确认", Toast.LENGTH_SHORT).show();

                        }
                    }
                }
                break;

        }

    }

    // 忘记密码 重新设置
    private void findtoword(String newWord) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/user/forgetPassword");
        params.addHeader("Content-Type","application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("password",newWord);
        jsonObject.put("mobile",phone);
        jsonObject.put("messageId",messageId);
        jsonObject.put("code",code);
        params.setBodyContent(jsonObject.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 密码--"+result);
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    Toast.makeText(ForgetInWordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                   // startActivity(new Intent(ForgetInWordActivity.this,UserLoginActivity.class));
                    finish();
                }else if (status.equals("0")){
                    String failure = obj.getString("failure");
                    if (failure.equals("1005")){
                        Toast.makeText(ForgetInWordActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                    }else if (failure.equals("1009")){
                        Toast.makeText(ForgetInWordActivity.this, "手机号未注册", Toast.LENGTH_SHORT).show();
                    }else if (failure.equals("1003")){
                        Toast.makeText(ForgetInWordActivity.this, "账号被禁用", Toast.LENGTH_SHORT).show();
                    }else {}
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 密码--"+ex);
                ErrType.errType(ForgetInWordActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });



    }

}
